package com.demo2do.fms.admin.service.supplier.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.supplier.DriverEventDao;
import com.demo2do.fms.admin.entity.search.supplier.DriverEventSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.supplier.DriverEventService;
import com.demo2do.fms.entity.driver.DriverEvent;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 *
 * @author Ark
 *
 */
@Service("driverEventService")
public class DriverEventServiceImpl extends DataServiceImpl<DriverEvent> implements DriverEventService {

    @Dao
    private DriverEventDao driverEventDao;
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.DriverEventService#count(com.demo2do.fms.admin.entity.security.Login)
     */
    public int count(Login login) {
        
        if (login.isSupplier()) {
            
            Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            return driverEventDao.count(supplier);
            
        } else {
            return driverEventDao.count();
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.DriverEventService#list(com.demo2do.fms.admin.entity.security.Login, com.demo2do.core.entity.page.Page)
     */
    public List<DriverEvent> list(Login login, Page page) {
        
        page.calculate();
        
        if (login.isSupplier()) {
            
            Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            return driverEventDao.list(supplier, page.getBeginIndex(), page.getEveryPage());
            
        } else {
            return driverEventDao.list(page.getBeginIndex(), page.getEveryPage());
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.DriverEventService#count(com.demo2do.fms.admin.entity.search.supplier.DriverEventSearch)
     */
    public int count(DriverEventSearch search) {
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return genericDaoSupport.count(sentence, parameters);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.DriverEventService#list(com.demo2do.fms.admin.entity.search.supplier.DriverEventSearch, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
    public List<DriverEvent> list(DriverEventSearch search, Page page) {
        
        // 1. build sentence
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        
        // 2. do query
        List<DriverEvent> events = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return events;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.DriverEventService#count(java.util.List)
     */
    public int count(List<Long> supplierIds) {
        if (CollectionUtils.isEmpty(supplierIds)) {
            return 0;
        }
        return driverEventDao.count(supplierIds);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.DriverEventService#list(java.util.List, com.demo2do.core.entity.page.Page)
     */
    public List<DriverEvent> list(List<Long> supplierIds, Page page) {
        List<DriverEvent> result = driverEventDao.list(supplierIds, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }
    
}
